import 'package:enjoy_plus_three/pages/Notfuned/index.dart';
import 'package:flutter/material.dart';

import '../pages/home/house_from.dart';
import '../pages/house/building_list.dart';
import '../pages/house/house_detail.dart';
import '../pages/house/house_list.dart';
import '../pages/house/location_list.dart';
import '../pages/house/room_list.dart';
import '../pages/login/index.dart';
import '../pages/notice/index.dart';
import '../pages/profile/index.dart';
import '../pages/tab_bar_page.dart';
import '../utils/token.dart';

class RouterManager {
  //配置路由
  static final Map<String, WidgetBuilder> routes = {
      '/': (context) => const TabBarPage(),
      '/detail':(context) => const NoticeDetail(),
      '/login' : (context) => const LoginPage(),
  };

  //拦截路由

  static Route<dynamic>onGenerateRoute (setting){
     //1.首先获取到token
      final tokenManager = TokenManager();
      final token = tokenManager.getToken() ?? '';
      //2.再进行判断,先判断token是否存在以及是否是前往登录页
      //如果token不存在,且前往的不是登录页,则跳转至登录页
      if(token.isEmpty && setting.name != '/login'){
        return MaterialPageRoute(builder: (context) => const LoginPage());
      }

      //如果上述代码没有执行,说明token存在
      //判断是否前往个人信息页
      if(setting.name == '/profile'){
        return MaterialPageRoute(builder: (context) => ProfilePage(userInfo: setting.arguments as Map,));
      }

      if (setting.name == '/house_list') {
        return MaterialPageRoute(
            builder: (context) =>
                const HouseList());
      }

        if (setting.name == '/location_list') {
        return MaterialPageRoute(builder: (context) => const LocationList());
      }

      if (setting.name == '/building_list') {
        return MaterialPageRoute(builder: (context) =>BuildingList(point: (setting.arguments as Map)['point']));
      }

      if (setting.name == '/room_list') {
        Map temp = setting.arguments as Map;
        return MaterialPageRoute(builder: (context) => RoomList(
          point: temp['point'],
          building: temp['building'],
        ));
      }

      if (setting.name == '/house_form') {
        return MaterialPageRoute(
          builder: (context) => HouseForm(
          params: setting.arguments as Map,
          )
        );
      }

      if (setting.name == '/house_detail') {
        return MaterialPageRoute(
        builder: (context) => HouseDetail(id: (setting.arguments as Map)['id']));
      }     

      //如果以上代码都没执行说明token存在,且不是前往登录页
      return MaterialPageRoute(builder: (context) => const notFound());
    
  }
}